// pages/index/pwd.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  change_pwd_click: function(e) {
    if (this.data.oldpwd == undefined || this.data.oldpwd.trim() == '') {
      wx.showToast({
        title: '请输入原密码',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    if (this.data.newPwd == undefined || this.data.newPwd.trim() == '') {
      wx.showToast({
        title: '请输入新密码',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    if (this.data.newPwd2 == undefined || this.data.newPwd2.trim() == '') {
      wx.showToast({
        title: '请输入重复新密码',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    let oldpwd = this.data.oldpwd.trim();
    let newPwd = this.data.newPwd.trim();
    let newPwd2 = this.data.newPwd2.trim();
    if (newPwd.length < 6) {
      wx.showToast({
        title: '新密码至少需要6位',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    if (newPwd != newPwd2) {
      wx.showToast({
        title: '新密码和重复新密码不一致',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    wx.request({
      url: getApp().data.servser + '/user/change/pwd',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      data: {
        account: wx.getStorageSync('username'),
        oldPassword: oldpwd,
        newPassword: newPwd
      },
      success: res => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '密码修改成功',
            icon: 'success',
            duration: 3000,
            success: function() {
              wx.setStorageSync('password', newPwd)
              let url = "/pages/index/user";
              wx.switchTab({
                url: url,
                success(res) {
                  let page = getCurrentPages().pop();
                  if (page == undefined || page == null) {
                    return
                  }
                  page.onShow();
                }
              })
            }
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 3000
          })
        }
      }
    })
  },
  getInputOldpwd: function(e) { //方法1
    this.data.oldpwd = e.detail.value;
  },
  getInputNewpwd: function(e) { //方法1
    this.data.newPwd = e.detail.value;
  },
  getInputNewRepeatpwd: function(e) { //方法1
    this.data.newPwd2 = e.detail.value;
  }
})